package edu.cth.scrabbit.client.view.panels;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import edu.cth.scrabbit.client.ctrl.ClientController;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConnectPanel extends javax.swing.JPanel {
	private JButton hostButton;
	private JButton startButton;
	private JButton joinButton;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public ConnectPanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(159, 101));
			{
				hostButton = new JButton();
				
				hostButton.setAction(getAppActionMap().get("hostGameAction"));
			}
			{
				joinButton = new JButton();
				
				joinButton.setAction(getAppActionMap().get("joinGameAction"));
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
			
			{
				startButton = new JButton();
				startButton.setName("startButton");
			}
			
			startButton.setAction(getAppActionMap().get("startGameAction"));
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(hostButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
				    .addComponent(joinButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(startButton, 0, 32, Short.MAX_VALUE)
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(hostButton, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(joinButton, 0, 63, Short.MAX_VALUE))
				    .addComponent(startButton, GroupLayout.Alignment.LEADING, 0, 136, Short.MAX_VALUE))
				.addContainerGap());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Non-generated code:
		hostButton.setText("Host");
		joinButton.setText("Join");
		startButton.setText("Start");
		startButton.setEnabled(false);
	}
	
    /**
    * Returns the action map used by this application.
     * Actions defined using the Action annotation
     * are returned by this method
     */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}
	
	@Action
	public void hostGameAction() {
		String nickName = JOptionPane.showInputDialog("Insert nickname");
		
		ClientController.getInstance().hostGame(nickName);
	}
	
	@Action
	public void joinGameAction() {
		String nickName = JOptionPane.showInputDialog("Insert nickname");
		String ip = JOptionPane.showInputDialog("Insert ip");

		ClientController.getInstance().joinGame(ip,nickName);
		
	}
	
	@Action
	public void startGameAction() {
		ClientController.getInstance().startGame();
	}

	public void setStartButtonEnabled(boolean enabled) {
		startButton.setEnabled(enabled);
	}
	
	public void setJoinButtonEnabled(boolean enabled) {
		joinButton.setEnabled(enabled);
	}

	public void setHostButtonEnabled(boolean enabled) {
		hostButton.setEnabled(enabled);
	}

	public void setConnectEnabled(boolean b) {
		joinButton.setEnabled(b);
		hostButton.setEnabled(b);
	}
}
